package com.manager.system.controller;

import com.manager.common.annotation.AspectLogInfo;
import com.manager.common.annotation.AspectMethodOperation;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.constant.OperationLogEnumType;
import com.manager.common.utils.DataTools;
import com.manager.common.utils.DateTools;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.Dictionary_Data;
import com.manager.system.service.DictionaryDataService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@RestController
@RequestMapping("/dictionary")
@SuppressWarnings("all")
public class DictionaryDataController{

	@Autowired
	private DictionaryDataService dictionaryDataService;
	
	/**
	 * TODO 判断统一组下是否有重名的数据
	 * @param model
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@PostMapping("/isDataExsit")
	public ResultVO isDataExsit(Dictionary_Data model, HttpServletRequest request, HttpServletResponse response) throws IOException{
		try {
			JSONObject obj=new JSONObject();
			boolean tag=dictionaryDataService.isDataExsit(model);
			if(tag){
				return ResultVO.failed("数据已经存在显示名称与值不能重复!");
			}
			return ResultVO.success();
		}catch (Exception e){

		}
		return ResultVO.failed("请求失败!");
	}
	
	@PostMapping("/getDicList")
	public PageVO getDicList(Dictionary_Data model, HttpServletRequest request, HttpServletResponse response) throws IOException{
		PageVO pageVO = dictionaryDataService.getDicMap(model);
		return pageVO;
	}
	
	@PostMapping("/insertDic")
	@AspectLogInfo(type= AspectMethodOperationEnum.ADD,tableName="tb_dictionary_data", content="添加字典表信息", logType = OperationLogEnumType.DICTIONARY)
	@AspectMethodOperation(operation = AspectMethodOperationEnum.ADD)
	public ResultVO insertDic(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		JSONObject json = new JSONObject();
		try{
			model.setShowTime(DateTools.getCurrentDateTime());
			model.setCreateTime(DateTools.getCurrentDateLong());
			dictionaryDataService.insertDic(model);
			return ResultVO.success();
		}catch(Exception e){
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
	@PostMapping("/updateDic")
	@AspectLogInfo(type= AspectMethodOperationEnum.UPDATE,tableName="tb_dictionary_data", content="修改字典表信息", logType = OperationLogEnumType.DICTIONARY)
	@AspectMethodOperation(operation = AspectMethodOperationEnum.UPDATE)
	public ResultVO updateDic(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		JSONObject json = new JSONObject();
		try{
			model.setShowTime(DateTools.getCurrentDateTime());
			model.setCreateTime(DateTools.getCurrentDateLong());
			dictionaryDataService.updateDic(model);
			return ResultVO.success();
		}catch(Exception e){
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
	@PostMapping("/deleteDic")
	@AspectMethodOperation(operation = AspectMethodOperationEnum.DELETE)
	@AspectLogInfo(type= AspectMethodOperationEnum.DELETE,tableName="tb_dictionary_data", content="删除字典表信息",logType = OperationLogEnumType.DICTIONARY)
	public ResultVO deleteDic(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		try{
			dictionaryDataService.deleteDic(model);
			return ResultVO.success();
		}catch(Exception e){
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
	
	@RequestMapping("/getDicType")
	public ResultVO getDicType(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Dictionary_Data> list = dictionaryDataService.getDicType();
		return ResultVO.success(list);
	}
	
	@RequestMapping("/getGroupMapDic")
	
	public Map<String, List<Dictionary_Data>> getGroupMapDic(Dictionary_Data model, HttpServletRequest request,HttpServletResponse response) throws IOException {
		return DataTools.dictionaryMap;
	}
	
	/**
	 * 根据分组标记和下拉值获取显示内容
	 * @param model
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getDicByGroupIdAndValue")
	public ResultVO getDicByGroupIdAndValue(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Dictionary_Data data = dictionaryDataService.getDicByGroupIdAndValue(model);
		return ResultVO.success(data);
	}

	/**
	 * 根据分组标记和显示内容获取下拉值
	 * @param model
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getDicByGroupIdAndText")
	public ResultVO getDicByGroupIdAndText(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Dictionary_Data data = dictionaryDataService.getDicByGroupIdAndText(model);
		return ResultVO.success(data);
	}

	@RequestMapping("/getAllDicData")
	public ResultVO getAllDicData(@RequestParam("pId") String pId,HttpServletResponse response, HttpServletRequest request) throws IOException{
		List<Dictionary_Data> list = dictionaryDataService.getAllDicData(pId);
		return ResultVO.success(list);
	}

	@RequestMapping("/getDicTreeDataByGroupId")
	public ResultVO getDicTreeDataByGroupId(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Dictionary_Data> list = dictionaryDataService.getDicTreeDataByGroupId(model);
		if("funds_source".equals(model.getGroupId())){
			list.forEach(new Consumer<Dictionary_Data>() {
				@Override
				public void accept(Dictionary_Data dictionary_data) {
					if(!"0".equals(dictionary_data.getpId())){
						dictionary_data.setText(dictionary_data.getText()+" - " + dictionary_data.getVal());
					}
				}
			});
		}
		return ResultVO.success(list);
	}

	@RequestMapping("/getDicTreeDataByGroupId2")
	public ResultVO getDicTreeDataByGroupId2(Dictionary_Data model, HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Dictionary_Data> list = dictionaryDataService.getDicTreeDataByGroupId(model);
		return ResultVO.success(list);
	}

	@RequestMapping("/getDataListByDicGroupId")
	public ResultVO getDataListByDicGroupId(@RequestParam("groupId") String groupId,HttpServletResponse response,HttpServletRequest request) throws Exception{
		List<Dictionary_Data> list = dictionaryDataService.getDataListByDicGroupId(groupId);
		return ResultVO.success(list);
	}

	@RequestMapping("/getChildrenDataByParentId")
	public ResultVO getChildrenDataByParentId(@RequestParam("parentId") String parentId, HttpServletRequest request,HttpServletResponse response) throws Exception{
		List<Dictionary_Data> list = dictionaryDataService.getChildrenDataByParentId(parentId);
		return ResultVO.success(list);
	}

	@RequestMapping("/getTreeDicData")
	public ResultVO getTreeDicData(Dictionary_Data model,HttpServletResponse response,HttpServletRequest request) throws  Exception{
		List<Dictionary_Data> list = dictionaryDataService.getTreeDicData();
		return ResultVO.success(list);
	}

	@RequestMapping("/getDicDataByGroupId")
	public ResultVO getChildDicDataList(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws Exception {
		List<Dictionary_Data> list = dictionaryDataService.getDicDataByGroupId(model.getGroupId());
		return ResultVO.success(list);
	}

	@RequestMapping("/getProcessItemData")
	public ResultVO getProcessItemData(Dictionary_Data model,HttpServletResponse response,HttpServletRequest request)throws Exception {
		JSONArray jsonArray = dictionaryDataService.getProcessItemData(model.getpId());
		return ResultVO.success(jsonArray);
	}

	@RequestMapping("/getMenuTreeDicData")
	public ResultVO getMenuTreeDicData(Dictionary_Data model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		List<Dictionary_Data> list = dictionaryDataService.getMenuTreeDicData();
		return ResultVO.success(list);
	}

	@RequestMapping("/getDicTreeData")
	public ResultVO getDicTreeData(Dictionary_Data model,HttpServletResponse response,HttpServletRequest request) throws Exception{
		try {
			List<Dictionary_Data> list = dictionaryDataService.getDicTreeData();
			return ResultVO.success(list);
		}catch (Exception e) {
			e.printStackTrace();
			return ResultVO.failed();
		}
	}
}
